package com.liu.nb.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.nb.entity.Role;
import com.liu.nb.entity.SysUser;
import com.liu.nb.service.IRoleService;
import com.liu.nb.service.IUserService;
import com.liu.nb.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.security.Principal;
import java.sql.Wrapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/nb/role")
public class RoleController {
    @Resource
    private IRoleService roleService;
    @Resource
    private IUserService userService;
    //获取角色列表
        @PostMapping("/getRole/{pageCurrent}/{pageSize}")
    public Result getRole(@PathVariable Integer pageCurrent,@PathVariable Integer pageSize,@RequestBody HashMap<String,String> map){
            Integer state=null;
            String name=map.get("name");
            String str= map.get("state");
            if(null!=str && str!=""){
                state = Integer.parseInt(str);
            }
        return Result.success(roleService.getRole(pageCurrent,pageSize,name,state));
    }
    //添加角色
    @PostMapping("/addrole")
    public Result addRole(@RequestBody Role role,Principal principal){
        SysUser user = userService.getByUsername(principal.getName());
        role.setCreateTime(LocalDateTime.now());
        role.setCreateUserId(user.getId());
        return Result.success(roleService.save(role));
    }
    //修改角色
    @PostMapping("/updrole")
    public Result updRole(@RequestBody Role role){
            role.setUpdateTime(LocalDateTime.now());
        return  Result.success(roleService.updateById(role));
    }
    //删除角色
    @PostMapping("/delrole/{id}")
    public Result deleteRole(@PathVariable int id){
        return Result.success(roleService.removeById(id));
    }
    //根据角色id获取角色
    @PostMapping("/getById/{id}")
    public Result getById(@PathVariable int id){
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("id", id));
        return Result.success(role.getPermsId());
    }
}
